package com.xudow.app.dynamicstate_old.module.user;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.jude.beam.bijection.Presenter;
import com.jude.library.imageprovider.ImageProvider;
import com.jude.library.imageprovider.OnImageSelectListener;
import com.jude.utils.JUtils;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.widget.ProgressDialogTransform;

import java.io.File;

/**
 * Created by ske on 2016/6/17.
 */
public class WriteIntroducePresenter extends Presenter<WriteIntroduceActivity> {
    private ImageProvider provider;
    private String face;
    private String info;
    private int type = 1;

    @Override
    protected void onCreate(WriteIntroduceActivity view, Bundle savedState) {
        super.onCreate(view, savedState);
        provider = new ImageProvider(getView());
    }

    public void setType(int type) {
        this.type = type;
    }

    OnImageSelectListener mFaceListener = new OnImageSelectListener() {

        @Override
        public void onImageSelect() {
            getView().getExpansion().showProgressDialog("加载中");
        }

        @Override
        public void onImageLoaded(Uri uri) {
            getView().getExpansion().dismissProgressDialog();
            getView().setFace(ImageProvider.readImageWithSize(uri, 300, 300));
            face = uri.getPath();
        }

        @Override
        public void onError() {

        }
    };

    OnImageSelectListener mInfoListener = new OnImageSelectListener() {

        @Override
        public void onImageSelect() {
            getView().getExpansion().showProgressDialog("加载中");
        }

        @Override
        public void onImageLoaded(Uri uri) {
            getView().getExpansion().dismissProgressDialog();
            getView().setInfo(ImageProvider.readImageWithSize(uri, 300, 300));
            info = uri.getPath();
        }

        @Override
        public void onError() {

        }
    };


    public void editFace(int type) {
        switch (type) {
            case 0:
                provider.getImageFromCamera(mFaceListener);
                break;
            case 1:
                provider.getImageFromAlbum(mFaceListener);
                break;
        }
    }

    public void editIntro(int type) {
        switch (type) {
            case 0:
                provider.getImageFromCamera(mInfoListener);
                break;
            case 1:
                provider.getImageFromAlbum(mInfoListener);
                break;
        }
    }

    public void submit() {
        if (TextUtils.isEmpty(face) || TextUtils.isEmpty(info)) {
            JUtils.Toast("请选择图片");
            return;
        }

        ImageModel.getInstance().putImageSync(new File(face))
                .flatMap(s -> {
                    face = s;
                    return ImageModel.getInstance().putImageSync(new File(info));
                }).flatMap(s -> DynamicModel.getInstance().uploadCertificate(face, s, type))
                .compose(new ProgressDialogTransform<>(getView(), "上传中"))
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .subscribe(message -> {
                    JUtils.Toast("提交成功");
                    getView().finish();
                });
    }

    @Override
    protected void onResult(int requestCode, int resultCode, Intent data) {
        super.onResult(requestCode, resultCode, data);
        provider.onActivityResult(requestCode, resultCode, data);
    }

}
